﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Http;
using Common.Logging;
using Learun.Application.Base.SystemModule;
using Learun.Application.Organization;
using Learun.Application.TwoDevelopment.ZZDT_EC;
using Learun.Loger;
using Learun.Util;
using Learun.Util.Operat;
using Learun.Util.SqlSugar;
using log4net.Config;
using Pipelines.Sockets.Unofficial.Arenas;

namespace Learun.Application.Web.AppApi
{
    /// <summary>
    /// 通知消息的接口（By YuXH）
    /// </summary>
    [RoutePrefix("api/NotificationApi")]
    [HandlerApiLogin(FilterMode.Ignore)]
    public class NotificationApiController : WebApiControllerBase
    {
        #region 图纸消息
        /// <summary>
        /// 为图纸最新的一个检入记录发起通知
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="Users">用户之间逗号分开</param>
        /// <param name="DrawingID"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerApiLogin(FilterMode.Enforce)]
        public IHttpActionResult Add(string ProjectId, string Users, string DrawingID)
        {
            var synRecord = new ec_drawing_synBLL().GetList("{DrawingFileID:\"" + DrawingID + "\",ProjectId:\"" + ProjectId + "\"}");
            var LatestSyn = synRecord.OrderByDescending(x => x.CreateTime).FirstOrDefault();
            if (LatestSyn != null)
            {
                foreach (var userName in Users.Replace("|", ",").Replace("，", ",").Split(','))
                {
                    ec_notificationEntity ec_NotificationEntity = new ec_notificationEntity
                    {
                        SenderUserID = LoginUserInfo.Get().realName,
                        RetrieveUserID = userName,
                        DrawingSynID = LatestSyn.DrawingSynID,
                        CheckFLG = 0
                    };

                    new ec_notificationBLL().SaveEntity("", ec_NotificationEntity, ProjectId);
                }
            }


            return Success("OK");
        }

        /// <summary>
        /// 获取某一个用户所有的未读通知
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="User">当前用户</param>
        /// <returns></returns>
        [HttpGet]
        [HandlerApiLogin(FilterMode.Ignore)]
        public IHttpActionResult GetUserAllNotification(string ProjectId, string User)
        {
            var res = new ec_notificationBLL().GetUserAllNotification(ProjectId, User);
            return Success(res);
        }

        /// <summary>
        /// 批量确认消息
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="IDs">逗号隔开</param>
        /// <returns></returns>
        [HttpGet]
        [HandlerApiLogin(FilterMode.Enforce)]
        public IHttpActionResult ApproveNofity(string ProjectId, string IDs)
        {
            new ec_notificationBLL().Approve(IDs, ProjectId);
            return Success("OK");
        }

        #endregion
        #region 捡入消息（需要生产确认的）
        /// <summary>
        /// 获取所有图纸，需要生产确认的检入记录
        /// </summary>
        /// <param name="projectId">项目ID</param>
        /// <param name="drawingFileID">图纸文件ID</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetCheckInLogNeedApprove(string projectId)
        {
            try
            {
                string queryJson = "{\"IsProcess\":\"" + "1" + "\",\"ProjectId\":\"" + projectId + "\"}";
                var listDrawingSyn = new ec_drawing_synBLL().GetList(queryJson);

                return Success(listDrawingSyn.GroupBy(x => x.DrawingFileID).ToList());
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 批量确认捡入记录里需要生产主管确认的
        /// </summary>
        /// <param name="ProjectId"></param>
        ///  <param name="userId">主管人的用户id</param>
        /// <param name="IDs">DrawingSynID,逗号隔开</param>
        /// <returns></returns>
        [HttpGet]
        [HandlerApiLogin(FilterMode.Enforce)]
        public IHttpActionResult ApproveCheckInLog(string ProjectId, string userId, string IDs)
        {
            var tbName = ProjectSugar.TableName<ec_drawing_synEntity>(ProjectId);
            SqlSugarHelper.Db.Updateable<ec_drawing_synEntity>().AS(tbName)
                .SetColumns(x => x.ApproveByAdmin == 1)
                .SetColumns(x => x.UpdateUserID == userId)
                .Where(x => IDs.Split(',').Contains(x.DrawingSynID))
                .ExecuteCommand();
            return Success("OK");
        }

        #endregion
    }
}